﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_Public;
using QLTV_BLL;

namespace GUI
{
    public partial class frmNguonCungCap : DevExpress.XtraEditors.XtraForm
    {
        #region Khai báo biến
        NguonCungCap_BLL ncc_bll = new NguonCungCap_BLL();
        DataTable dt = new DataTable();
        TextMessageBox box = new TextMessageBox();
        public string maNguonCung = "";
        private bool isOk = false;
        #endregion

        #region LoadForm

        public frmNguonCungCap()
        {
            InitializeComponent();
        }

        private void frmNguonCungCap_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        #endregion

        #region Các chức năng

        private void btnLuuThoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            isOk = true;
            updateData();
        }

        private void btnLuu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            isOk = false;
            updateData();
        }

        private void btnHuy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SetInterface();
        }

        private void btnThoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DialogResult = DialogResult.OK;
        }
        #endregion

        #region Các sự kiện

        private void frmNguonCungCap_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.Cancel == false)
                DialogResult = DialogResult.OK;
        }
        #endregion

        #region Hàm viết thêm
        private void updateData()
        {
            if (checkValid())
            {
                NguonCungCap_Public ncc = new NguonCungCap_Public();
                if (maNguonCung == "")
                {
                    if (checkTenInsert())
                    {
                        ncc.MaNguonCung = box.AutoID("NCC", 3, ncc_bll.LayDuLieu());
                        ncc.TenNguonCung = box.toUpper(txtTenNC.Text);
                        ncc.GhiChu = txtGhiChu.Text;

                        ncc_bll.Insert(ncc, ncc, ncc);
                        CloseForm();
                    }
                }
                else
                {
                    ncc.MaNguonCung = txtMaNC.Text.Trim();
                    ncc.TenNguonCung =box.toUpper(txtTenNC.Text);
                    ncc.GhiChu = txtGhiChu.Text;

                    ncc_bll.Update(ncc, ncc, ncc);
                    CloseForm();
                }
            }
        }

        private void CloseForm()
        {
            if (isOk == true)
                DialogResult = DialogResult.OK;
            else
                SetInterface();
        }

        private void LoadData()
        {
            if (maNguonCung != "")
            {
                dt = ncc_bll.TaoBang(box.sqlCheckExist("DM_NguonCungCap","MaNguonCung",maNguonCung));
                DataRow dr = dt.Rows[0];
                txtMaNC.Text = maNguonCung;
                txtTenNC.Text = dr["TenNguonCung"].ToString();
                txtGhiChu.Text = dr["GhiChu"].ToString();
            }
        }

        private void SetInterface()
        {
            txtMaNC.Text = "Mã tự sinh";
            txtTenNC.Text = "";
            txtGhiChu.Text = "";
            txtTenNC.Focus();
            maNguonCung = "";
        }
        private bool checkValid()
        {
            if (txtTenNC.Text.Trim() == "")
            {
                XtraMessageBox.Show("Bạn chưa nhập Tên nguồn cung.",box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
                txtTenNC.Focus();
                return false;
            }
            return true;

        }
        private bool checkTenInsert()
        {
            dt = ncc_bll.TaoBang(box.sqlCheckExist("DM_NguonCungCap","TenNguonCung",txtTenNC.Text.Trim()));
            if (dt.Rows.Count > 0)
            {
                XtraMessageBox.Show(box.Exist(txtTenNC.Text),box.Caption,MessageBoxButtons.OK,MessageBoxIcon.Warning);
                txtTenNC.Focus();
                return false;
            }
            return true;
        }
        #endregion
    }
}